
<jsp:useBean id="sendbean" class="dasregistry.sendMailToFriendBean" scope="session"/>
<jsp:setProperty name="sendbean" property="*"/>

<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasSource"/>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasCoordinateSystem"/>
<jsp:directive.page import="org.biojava.services.das.registry.DasClient" />

<jsp:directive.page import="java.lang.Integer"/>
<jsp:directive.page import="java.util.Date"/>
<jsp:directive.page import="java.util.Map"/>
<jsp:directive.page import="java.text.SimpleDateFormat"/>
<jsp:directive.page import="java.util.regex.*"/>
<jsp:directive.page import="javax.mail.MessagingException"/>

<%
// fetch by auto id
   String auto_id = request.getParameter("auto_id");
   
   DasSource ds = sendbean.getDasSource(auto_id);
   if ( (auto_id == null ) || (ds == null) ) {
    String redirectURL = "sendToFriend.jsp?message=no_auto_id_was_provided";
    response.sendRedirect(redirectURL);
   
   }

   // email test
   String email   =  sendbean.getSendTo();
   if ( ( email == null ) || (email.equals("")) ) {
    String redirectURL = "sendToFriend.jsp?message=no_sendto_email_was_provided"+"&auto_id="+auto_id;
    response.sendRedirect(redirectURL);
   }
   
   Pattern   emailPattern = Pattern.compile("^[A-Za-z0-9\\._%-]+@[A-Za-z0-9\\._%-]+\\.[A-Za-z]{2,4}$");
   //Pattern   emailPattern = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
   Matcher m = emailPattern.matcher(email);	
   if (! m.matches() ) {
      String redirectURL = "sendToFriend.jsp?message=wrong_format_of_sendto_email"+"&auto_id="+auto_id;
      response.sendRedirect(redirectURL);
   
   }

   String myEmail = sendbean.getMyEmail();
   if ( (myEmail == null ) || (myEmail.equals("")) ) {
    String redirectURL = "sendToFriend.jsp?message=your_email_was_not_provided"+"&auto_id="+auto_id;
    response.sendRedirect(redirectURL);   
   }
   m = emailPattern.matcher(myEmail);	
   if (! m.matches() ) {
      String redirectURL = "sendToFriend.jsp?message=your_email_is_in_wrong_format"+"&auto_id="+auto_id;
      response.sendRedirect(redirectURL);
   }else{
    
   
   try  {
      sendbean.sendMail(ds,auto_id, email, myEmail);
     
      String redirectURL = "sendToFriend.jsp?message=message_sent_successfully"+"&auto_id="+auto_id;
      response.sendRedirect(redirectURL);
   

   } catch (MessagingException e) {
	 
     out.print(e.getMessage());
   }
   }
%>
